package net.seehope.foodie.controller.Intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

//这是一种由springMvc提倡的一种拦截组件，也算是控制器的一种

@Controller
public class TimeIntercepter implements HandlerInterceptor {
	private static final Logger log = LoggerFactory.getLogger(TimeIntercepter.class);
	
	//前置拦截
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		long start = System.currentTimeMillis();
		log.info("TimeIntercepter start:" + start);
		//塞入作用域当中
		request.setAttribute("start", start);
		return true;
	}

	//正常结束的拦截
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	//最终拦截
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		//从作用域中取出来
		long start = (long) request.getAttribute("start");
		long end = System.currentTimeMillis();
		
		log.info("TimeIntercepter end:" + end + " cost:" + (end-start));
	}



	
	
}
